// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html


cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        cow_prefab: {
            type: cc.Prefab,
            default: null,
        },
        nativePosY: -550,
        maxPosY: 26,
        ropMoveSpeed: 0.5,
        catchDistance: 5,

    },
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        this.cowArr = [];
        this.rope = this.node.getChildByName("bg").getChildByName("rope");
        this.rope.setLocalZOrder(1000);
        this.rope.y = this.nativePosY;
        this.btn_press = this.node.getChildByName("bg").getChildByName("ButtonPress");
        this.btn_press.setLocalZOrder(2000);
    },
    start() {
        this.create_cow();
    },
    create_cow: function () {
        var goCow = cc.instantiate(this.cow_prefab);
        this.node.getChildByName("bg").addChild(goCow);
        goCow.active = true;
        goCow.x = 544;
        goCow.y = -131;
        this.cowArr.push(goCow)
        this.scheduleOnce(this.create_cow.bind(this), Math.random() * 5 + 4);
    },
    update: function (dt) {

        this.checkIsCatchCow();


    },
    removeFrom_CowArr: function (cow) {
        var ind = this.cowArr.indexOf(cow);
        this.cowArr.splice(ind, 1);
    },

    ropeShoot: function () {
        var isCatch = false;
        var act = cc.moveTo(this.ropMoveSpeed, cc.p(this.rope.x, this.maxPosY));
        var act1 = cc.callFunc(function () {
            this.btn_press.getComponent(cc.Button).interactable = false;
            this.btn_press.enableAutoGrayEffect = true;
        }.bind(this), this.node);
        var act4 = cc.moveTo(this.ropMoveSpeed, cc.p(this.rope.x, this.rope.y));
        var act5 = cc.callFunc(function () {
            this.btn_press.getComponent(cc.Button).interactable = true;
            this.btn_press.enableAutoGrayEffect = false;
        }.bind(this), this.node);
        var act2, ac
        if (isCatch) {
            act2 = cc.callFunc(function () {
                catchFun();
            }.bind(this), this.node);
            ac = cc.sequence(act1, act, act2, act4, act5);
        } else {
            ac = cc.sequence(act1, act, act4, act5);
        }

        this.rope.runAction(ac);
    },
    ropeBack: function () {
        var act1 = cc.moveTo(this.ropMoveSpeed, cc.p(this.rope.x, this.rope.y));
        var act2 = cc.callFunc(function () {
            this.btn_press.getComponent(cc.Button).interactable = true;
            this.btn_press.enableAutoGrayEffect = false;

        }.bind(this), this.node);
        var ac = cc.sequence(act1, act2);

        this.rope.runAction(ac);


    },

    checkIsCatchCow: function () {

        var ropPosY = this.rope.y
        if (ropPosY >= this.maxPosY - 5) {

            var ropePosX = this.rope.x;
            for (var i = 0; i < this.cowArr.length; i++) {
                var item = this.cowArr[i]
                var cX = item.x;
                var cY = item.y;
                var v = cc.v2(Math.abs(Math.abs(cX) - Math.abs(ropePosX)), Math.abs(Math.abs(cY) - Math.abs(ropPosY)));
                var distance = v.mag();
                console.log("获得的距离是：", distance);
                if (distance < this.catchDistance) {
                    console.log("I Have actch the cow", distance);
                    return true;

                };
            };
            return false;
        };

    },
});
